home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 424_01 / ed_157 / journal.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-16  |  6.7 KB  |  259 lines

  1. /*
  2.  * Copyright (C) 1992 by Rush Record (rhr@clio.rice.edu)
  3.  * 
  4.  * This file is part of ED.
  5.  * 
  6.  * ED is free software; you can redistribute it and/or modify it under the terms
  7.  * of the GNU General Public License as published by the Free Software Foundation.
  8.  * 
  9.  * ED is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  10.  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  11.  * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  12.  * 
  13.  * You should have received a copy of the GNU General Public License along with ED
  14.  * (see the file COPYING).  If not, write to the Free Software Foundation, 675
  15.  * Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17. #include "opsys.h"
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23. #include "rec.h"
  24. #include "window.h"
  25. #include "ed_dec.h"
  26. #include "buffer.h"
  27. #include "buf_dec.h"
  28.  
  29. static FILE *fp = NULL;
  30. static Int pos = 0;
  31. static Char buf[128];    /* room for ten keys */
  32. static Char jfilename[512];    /* saved journal file name, used to reopen the journal after a recovery */
  33. struct inhibit_flags
  34. {
  35.     unsigned int search:1;
  36.     unsigned int paste:1;
  37.     unsigned int line:1;
  38.     unsigned int word:1;
  39.     unsigned int chr:1;
  40. };
  41.  
  42. /******************************************************************************\
  43. |Routine: journal
  44. |Callby: init_term
  45. |Purpose: Stores characters in the journal file.
  46. |Arguments:
  47. |    c is the character to store.
  48. |    repeat is the repeat count to associate with the character.
  49. \******************************************************************************/
  50. void journal(c,repeat)
  51. Char c;
  52. Int repeat;
  53. {
  54.     if(!fp)
  55.         return;
  56.     sprintf(buf + pos,"%x %x ",(Uchar)c,repeat);
  57.     pos += strlen(buf + pos);
  58.     if(pos > sizeof(buf) - 14)
  59.     {
  60.         buf[pos] = '\0';
  61.         fprintf(fp,"%s\n",buf);
  62.         fflush(fp);
  63.         pos = 0;
  64.     }
  65. }
  66.  
  67. /******************************************************************************\
  68. |Routine: journal_fini
  69. |Callby: main
  70. |Purpose: Closes the journal file, and (on VMS systems) purges it to one version.
  71. |Arguments:
  72. |    none
  73. \******************************************************************************/
  74. void journal_fini()
  75. {
  76. #ifdef VMS
  77.     Char journal_file[512];
  78. #endif
  79.  
  80.     if(!fp)
  81.         return;
  82.     buf[pos] = '\0';
  83.     fprintf(fp,"%s\n",buf);
  84.     fflush(fp);
  85.     fclose(fp);
  86.     fp = NULL;
  87. #ifdef VMS
  88.     strcpy(journal_file,JOURNAL_FILE);
  89.     strcat(journal_file,";-1");
  90.     while(!remove(journal_file));
  91. #endif
  92. }
  93.  
  94. /******************************************************************************\
  95. |Routine: journal_buffer
  96. |Callby: journal
  97. |Purpose: Stores a buffer in the journal file.
  98. |Arguments:
  99. |    prefix is the prefix for the string.
  100. |    buffer is the buffer pointer.
  101. \******************************************************************************/
  102. void journal_buffer(prefix,buffer)
  103. Char *prefix;
  104. buf_ptr buffer;
  105. {
  106.     static Char *zip = (Char *)"\0";
  107.     
  108.     if(!fp)
  109.         return;
  110.     fprintf(fp,"%s=%s\n",prefix,(
  111.         buffer->first == (rec_ptr)&buffer->first ||
  112.         !buffer->first->length ||
  113.         !buffer->first->data)? zip : buffer->first->data);
  114. }
  115.  
  116. /******************************************************************************\
  117. |Routine: journal_init
  118. |Callby: main
  119. |Purpose: Creates a journal file.
  120. |Arguments:
  121. |    file is the name the journal file should have.
  122. \******************************************************************************/
  123. void journal_init(file)
  124. Char *file;
  125. {
  126.     Char buf[512];
  127.     
  128.     strcpy(buf,JOURNAL_FILE);
  129.     strip_quotes(buf);
  130.     envir_subs(buf);
  131.     if(!(fp = fopen(buf,"w")))
  132.         return;
  133.     fprintf(fp,"%s\n",file);
  134.     journal_buffer("search",&SEARCHBUF);
  135.     journal_buffer("paste",&KILLBUF);
  136.     journal_buffer("line",&LINEBUF);
  137.     journal_buffer("word",&WORDBUF);
  138.     journal_buffer("char",&CHARBUF);
  139.     pos = 0;
  140. }
  141.  
  142. /******************************************************************************\
  143. |Routine: unjournal
  144. |Callby: init_term
  145. |Purpose: Retrieves a character and repeat count from the journal. Closes the
  146. |         journal file when it runs out.
  147. |Arguments:
  148. |    c is the returned character.
  149. |    repeat is the associated repeat count.
  150. \******************************************************************************/
  151. void unjournal(c,repeat)
  152. Char *c;
  153. Int *repeat;
  154. {
  155.     Int ic,ir,n;
  156.     Char buf[512];
  157.  
  158.     n = my_fscanf(fp,"%x%x",&ic,&ir);
  159.     if(n < 0)
  160.     {
  161.         pos = 0;
  162.         fclose(fp);
  163.         strcpy(buf,jfilename);
  164.         strip_quotes(buf);
  165.         envir_subs(buf);
  166.         if(!(fp = fopen(buf,"a")))
  167.         {
  168.             printf("Unable to append to the journal file %s.\r\n",buf);
  169.             cleanup(-1);
  170.         }
  171.         *repeat = 0;
  172.         return;
  173.     }
  174.     else if(n != 2)
  175.     {
  176.         printf("There is an error in the journal file %s.\r\n",JOURNAL_FILE);
  177.         cleanup(-1);
  178.     }
  179.     *((Schar *)c) = ic;
  180.     *repeat = ir;
  181. }
  182.  
  183. /******************************************************************************\
  184. |Routine: unjournal_buffer
  185. |Callby: journal
  186. |Purpose: Recalls a buffer from the journal file.
  187. |Arguments:
  188. |    prefix is the prefix for the string.
  189. |    buffer is the buffer pointer.
  190. |    inhibit is the set of buffer-loading inhibition flags.
  191. \******************************************************************************/
  192. void unjournal_buffer(prefix,buffer,inhibit)
  193. Char *prefix;
  194. buf_ptr buffer;
  195. Int inhibit;
  196. {
  197.     Int l;
  198.     Char string[512];
  199.  
  200.     if(!fgets(string,sizeof(string),fp))
  201.     {
  202.         printf("Unexpected end-of-file or error while reading the %s buffer from the journal file.\r\n",prefix);
  203.         cleanup(-1);
  204.     }
  205.     l = strlen(prefix);
  206.     if(strncmp(string,prefix,l))
  207.     {
  208.         printf("Was expecting %s=<string> in journal file, found %s.\r\n",prefix,string);
  209.         cleanup(-1);
  210.     }
  211.     if(!inhibit)
  212.     {
  213.         *strchr(string,'\n') = '\0';
  214.         string_to_buf(string + l + 1,buffer);
  215.     }
  216. }
  217.  
  218. /******************************************************************************\
  219. |Routine: unjournal_init
  220. |Callby: parse_fnm
  221. |Purpose: Opens an existing journal file and retrieves the name of the file
  222. |         that was being edited.
  223. |Arguments:
  224. |    file is the returned name of the file being edited.
  225. \******************************************************************************/
  226. void unjournal_init(jfile,file,inhibit)
  227. Char *jfile,*file;
  228. struct inhibit_flags *inhibit;
  229. {
  230.     Char buf[512],lfile[512];
  231.     
  232.     if(fp)
  233.         fclose(fp);    /* if text processing on multiple files, close journal before reopening it */
  234.     strcpy(lfile,jfile);
  235.     strip_quotes(lfile);
  236.     envir_subs(lfile);
  237.     if(!(fp = fopen(lfile,"r")))
  238.     {
  239.         printf("Unable to open the journal file %s.\r\n",lfile);
  240.         cleanup(-1);
  241.     }
  242.     if(!fgets(buf,sizeof(buf),fp))
  243.     {
  244.         printf("Unable to read the edited file name from the journal file %s.\r\n",lfile);
  245.         cleanup(-1);
  246.     }
  247.     *strchr(buf,'\n') = '\0';
  248.     strcpy(file,buf);
  249.     unjournal_buffer("search",&SEARCHBUF,inhibit->search);
  250.     unjournal_buffer("paste",&KILLBUF,inhibit->paste);
  251.     unjournal_buffer("line",&LINEBUF,inhibit->line);
  252.     unjournal_buffer("word",&WORDBUF,inhibit->word);
  253.     unjournal_buffer("char",&CHARBUF,inhibit->chr);
  254.     pos = 0;
  255.     set_recover();    /* tell get_next_key to get from journal file */
  256.     strcpy(jfilename,lfile);
  257. }
  258.  
  259.